package com.ncmmall.mall.controller.business.order;

import com.ncmmall.application.business.order.BillLadingApplication;
import com.feijin.commons.exceptions.IllegalRequestException;
import com.feijin.commons.webs.Webs;
import com.ncmmall.domain.business.express.Express;
import com.ncmmall.domain.business.express.QExpress;
import com.ncmmall.domain.business.order.lading.BillOfLading;
import com.ncmmall.domain.business.order.lading.QBillOfLading;
import com.ncmmall.mall.controller.securitys.shiro.Securitys;
import com.ncmmall.mall.params.PageParams;
import com.ncmmall.querychannel.QueryPage;
import com.ncmmall.querychannel.QueryPageRequest;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * @author linxs
 * @Description: 提货单管理
 * @Date: 2020-04-29 23:45
 */
@Controller
@RequestMapping(value = "/supplier/billLading")
public class SupplierBillLadingController {

    private final static Logger logger = LoggerFactory.getLogger(SupplierBillLadingController.class);

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private BillLadingApplication billLadingApplication;

    /**
     * 订单列表
     *
     * @param model
     * @param pageParams
     * @param order
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String list(Model model, PageParams pageParams, BillOfLading merchant) throws ParseException {
        QueryPageRequest pageRequest = new QueryPageRequest().setPageNo(pageParams.getPageNo());
        Map<String, Object> params = Maps.newHashMap();
        params.put("id", Securitys.currentUser().getId());

        String hql = builderSQL("select b from BillOfLading b where  b.supplier.id = :id  and b.status != 1 ", params, merchant);
        QueryPage<BillOfLading> page = queryChannelService.findPage(hql, pageRequest.setParams(params), BillOfLading.class);

        model.addAttribute("page", page).addAttribute("params", merchant);

        return "supplier/billLading/list";
    }


    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public String details(Model model, @RequestParam("id") long id) {

        model.addAttribute("billLading", queryChannelService.findOne(QBillOfLading.billOfLading.id.eq(id), BillOfLading.class));

        return "supplier/billLading/details";
    }

    /**
     * 去发货
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping(value = "/{id}/delivery", method = RequestMethod.GET)
    private String delivery(Model model, @PathVariable("id") long id) {

        BillOfLading billOfLading = queryChannelService.findOne(QBillOfLading.billOfLading.id.eq(id), BillOfLading.class);
        List<Express> expresses = queryChannelService.findAll(QExpress.express.status.eq(2), Express.class);

        model.addAttribute("billOfLading", billOfLading).addAttribute("expresses", expresses).addAttribute("_method", "PUT");

        return "supplier/billLading/delivery";
    }


    /**
     * 发货
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/{id}/delivery", method = RequestMethod.PUT)
    @ResponseBody
    private Map delivery(HttpServletRequest request, @RequestHeader("X-Requested-With") String requestWith, BillOfLading params) {
        try {
            if (!Webs.isAjaxRequest(requestWith)) {
                throw new IllegalRequestException("request must be Ajax!");
            }

            billLadingApplication.delivery(params);

            return ImmutableMap.of("status", true);

        } catch (Exception e) {
            logger.error("[发货异常]", e);
            return ImmutableMap.of("status", false, "message", "发货失败");
        }
    }

    private String builderSQL(String sql, Map<String, Object> params, BillOfLading merchant) {
        return sql;
    }

}
